package com.banyou.banyou.order.tourjourney;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banyou.banyou.R;
import com.banyou.banyou.base.BaseActivity;
import com.banyou.banyou.bean.journey.tourinfo.TourInfoBean;
import com.banyou.banyou.order.evaluate.EvaluateActivity;
import com.banyou.banyou.order.evaluatehistory.EvaluateHistoryActivity;
import com.banyou.banyou.util.Constants;
import com.banyou.banyou.util.DialogFactory;
import com.banyou.banyou.util.GlideUtil;
import com.banyou.banyou.util.RxBus;
import com.banyou.banyou.util.ShowToast;
import com.socks.library.KLog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * 类描述：伴游，查看订单详情
 * 创建人：Random
 * 修改时间：
 * 修改备注：
 */

public class TourJourneyActivity extends BaseActivity<TourJourneyPresenter> implements TourJourneyContract.View {

    @BindView(R.id.back_btn)
    ImageView mBackBtn;
    @BindView(R.id.title_bar)
    RelativeLayout mTitleBar;
    @BindView(R.id.main_img)
    ImageView mMainImg;
    @BindView(R.id.name)
    TextView mName;
    @BindView(R.id.sex)
    TextView mSex;
    @BindView(R.id.age)
    TextView mAge;
    @BindView(R.id.star_content)
    LinearLayout mStarContent;
    @BindView(R.id.short_msg)
    TextView mShortMsg;
    @BindView(R.id.head_content)
    RelativeLayout mHeadContent;
    @BindView(R.id.origin_time)
    TextView mOriginTime;
    @BindView(R.id.total_price)
    TextView mTotalPrice;
    @BindView(R.id.order_state)
    TextView mOrderState;
    @BindView(R.id.cancle_btn)
    Button mCancleBtn;
    @BindView(R.id.end_btn)
    Button mEndBtn;
    @BindView(R.id.order_remark)
    TextView mOrderRemark;
    @BindView(R.id.daily_price)
    TextView mDailyPrice;
    @BindView(R.id.insurance_price)
    TextView mInsurancePrice;
    @BindView(R.id.checkout_speak)
    Button mCheckoutSpeak;

    private long mOrderId;
    private String mCOrderState;

    @Override
    protected TourJourneyPresenter initPresenter() {
        return new TourJourneyPresenter();
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_journey_info;
    }

    @Override
    protected View initLayoutView() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter.initPresenter(this, new TourJourneyModel());
        ButterKnife.bind(this);
        mCancleBtn.setText("不接单");
        mEndBtn.setText("接单");
        mOrderId = getIntent().getLongExtra("orderId", 0);
        mCOrderState = getIntent().getStringExtra("orderState");
        if (!mCOrderState.equals("2")) {
            mEndBtn.setVisibility(View.GONE);
            mCancleBtn.setVisibility(View.GONE);
        }
        if(!mCOrderState.equals("5")){
            mCheckoutSpeak.setVisibility(View.GONE);
        }
        mPresenter.getData(mOrderId + "");
    }

    @Override
    protected String pageName() {
        return "伴游行程单详情";
    }


    @OnClick({R.id.back_btn, R.id.cancle_btn, R.id.end_btn,R.id.checkout_speak})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                this.finish();
                RxBus.getInstance().send(Constants.REFRESH_LIST_FRAGMENT);

                break;
            case R.id.cancle_btn:
                createCancleOrderDialog();
                break;
            case R.id.end_btn://接单
                if (mCOrderState.equals("2")) {
                    createTakeOrderDialog();
                } else if (mCOrderState.equals("5")) {
                    endSuccess();
                } else {
                    mPresenter.end(mOrderId + "", "2");
                }
                break;
            case R.id.checkout_speak:
                Intent intent=new Intent(this, EvaluateHistoryActivity.class);
                intent.putExtra("orderId",mOrderId);
                startActivity(intent);
                break;
        }
    }

    private AlertDialog mTakeDialog, mCancleDialog;

    /**
     * 创建取消接单Dialog
     */
    private void createCancleOrderDialog() {
        DialogFactory factory = new DialogFactory(this);
        mCancleDialog = factory.createDialog("确定取消接单么？");
        factory.setCancleBtnText("取消");
        factory.setSureBtnText("确定");
        factory.setOnBtnClickListener(new DialogFactory.OnBtnClickListener() {
            @Override
            public void onSureClick(View view) {
                mCancleDialog.dismiss();
                mPresenter.cancleOrder(mOrderId + "");
            }

            @Override
            public void onCancleClick(View view) {
                mCancleDialog.dismiss();

            }
        });
        mCancleDialog.show();
    }


    /**
     * 创建接单dialog
     */
    private void createTakeOrderDialog() {
        DialogFactory factory = new DialogFactory(this);
        mTakeDialog = factory.createDialog("确定接单么？");
        factory.setCancleBtnText("取消");
        factory.setSureBtnText("确定");
        factory.setOnBtnClickListener(new DialogFactory.OnBtnClickListener() {
            @Override
            public void onSureClick(View view) {
                mTakeDialog.dismiss();
                mPresenter.takeOrder(mOrderId + "");
            }

            @Override
            public void onCancleClick(View view) {
                mTakeDialog.dismiss();
            }
        });
        mTakeDialog.show();
    }

    /**
     * 获取数据成功
     *
     * @param bean
     */
    @Override
    public void getDataSuccess(TourInfoBean bean) {
        mName.setText("游客姓名：" + bean.getOrderInfo().getSysUser().getUserName());
        mAge.setText("年龄：" + bean.getOrderInfo().getSysUser().getAttributes().getAGE());
        if (bean.getOrderInfo().getSysUser().getSex().equals("M")) {
            mSex.setText("性别：男");
        } else {
            mSex.setText("性别：女");
        }
        mStarContent.removeAllViews();
        mInsurancePrice.setText("保险费用：" + bean.getOrderInfo().getSysUserInsurance().getInsuranceBalance() + "￥");
        GlideUtil.circleLocalLoad(this, bean.getOrderInfo().getSysUser().getAttributes().getPORTAL_IMAG_PATH(), mMainImg);
        mTotalPrice.setText("费用总计：" + bean.getOrderInfo().getTotalPrice());
        mOrderRemark.setText("订单备注：" + bean.getOrderInfo().getRemarks());
        mOriginTime.setText("订单日程：" + bean.getOrderInfo().getOrderDates());
        mShortMsg.setText("");
        if (mCOrderState.equals("1")) {
            mOrderState.setText("订单状态：待付款");
            mEndBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ShowToast.showToast("请等待用户付款");
                }
            });
        } else if (mCOrderState.equals("2")) {
            mOrderState.setText("订单状态：待接单");
        } else if (mCOrderState.equals("3") || mCOrderState.equals("4")) {
            mOrderState.setText("订单状态：进行中");
            mEndBtn.setVisibility(View.VISIBLE);
            mCancleBtn.setVisibility(View.VISIBLE);
            mCancleBtn.setText("取消订单");
            mEndBtn.setText("结束订单");
        } else if (mCOrderState.equals("5")) {
            mOrderState.setText("订单状态：已完成");
            mEndBtn.setVisibility(View.VISIBLE);
            mEndBtn.setText("点击评价");
            mCancleBtn.setVisibility(View.GONE);
        } else if (mCOrderState.equals("7")) {
            mOrderState.setText("订单状态：审核中");
            mEndBtn.setVisibility(View.GONE);
            mCancleBtn.setVisibility(View.GONE);
        } else if (mCOrderState.equals("8")) {
            mOrderState.setText("订单状态：已取消");
            mEndBtn.setVisibility(View.GONE);
            mCancleBtn.setVisibility(View.GONE);
        } else if (mCOrderState.equals("9")) {
            mOrderState.setText("订单状态：审核不通过");
            mEndBtn.setVisibility(View.GONE);
            mCancleBtn.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.getInstance().send(Constants.REFRESH_LIST_FRAGMENT);
    }

    /**
     * 接单成功
     */
    @Override
    public void takeOrderSuccess() {
        ShowToast.showToast("接单成功");
        finish();
    }

    /**
     * 取消订单
     */
    @Override
    public void cancleOrderSuccess() {
        ShowToast.showToast("取消成功");
        finish();
        RxBus.getInstance().send(Constants.REFRESH_LIST_FRAGMENT);
    }

    /**
     * 结束订单
     */
    @Override
    public void endSuccess() {
        RxBus.getInstance().send(Constants.REFRESH_LIST_FRAGMENT);
        ShowToast.showToast("结束成功，请评价");
        Intent intent = new Intent(this, EvaluateActivity.class);
        KLog.i("orderId=" + mOrderId);
        intent.putExtra("orderId", mOrderId);
        startActivity(intent);
    }

}
